import { createStore } from 'vuex'
import VueCookies from 'vue-cookies'
import { passwordLogin, phoneRegister, emailRegister, phoneCodeLogin, phoneRetrieve, emailRetrieve, checkUserExist } from "@/request/api"
import router from "@/router/index"
import { toast } from '@/components/toast'
// import { getCurrentInstance } from 'vue'
//const xxxxx = internalData.$cookies.get('xxxxx') // 后面的为之前设置的cookies的名字
// if (ret.code === 200) {
//   // 删除之前的cookies
//   if ($cookies.isKey("xxxxx")) {
//     $cookies.remove("xxxxx")
//   }
// }
// 设置cookies保存的内容
// const xxxxx = {
//   username: '',
//   password: '',
//   // 以下还有很多信息
// }
// 设置cookies的日期为一个月
// $cookies.config("1m")
// // 设置cookies
// $cookies.set("xxxxx", xxxxx); // 前面的为设置cookies的名字，后面为内容


export default createStore({
  state: {
    webp: true,
    message: false,
    myInfo: $cookies.get('myInfo'),
    token: $cookies.get('token'),
    nickname: $cookies.get('nickname'),
    avatar: $cookies.get('avatar'),
    backRouter: localStorage.getItem("backRoute") || '/',
    routineList: {},
    bacteriaList: {},
    proteinList: {},
    automaticLogin: eval(localStorage.getItem('automaticLogin')) || false
  },
  getters: {
    isWebp(state) {
      return state.webp
    },
    messageShow(state) {
      return state.message
    },
    token(state) {
      return state.token
    },
    automaticLogin(state) {
      return state.automaticLogin
    }
  },
  mutations: {
    setRoutine(state, value) {
      state.routineList = value
    },
    setBacteria(state, value) {
      state.bacteriaList = value
    },
    setProtein(state, value) {
      state.proteinList = value
    },
    addIsWebp(state, value) {
      state.webp = value
    },
    addMessage(state, value) {
      state.message = value
    },
    setConfirmShow(state, value) {
      state.confirm.show = false;
    },
    setToken(state, value) {
      state.token = value
      if (state.automaticLogin) {
        if ($cookies.isKey("token")) {
          $cookies.remove("token")
        }
        $cookies.config("15d")
        $cookies.set("token", value);
      } else {
        if ($cookies.isKey("token")) {
          $cookies.remove("token")
        }
        $cookies.set("token", value);
      }
    },
    setNickname(state, value) {
      state.nickname = value;
      if ($cookies.isKey("nickname")) {
        $cookies.remove("nickname")
      }
      $cookies.config("7d")
      $cookies.set("nickname", value);
    },
    setAvatar(state, value) {
      state.avatar = value;
      if ($cookies.isKey("avatar")) {
        $cookies.remove("avatar")
      }
      $cookies.config("7d")
      $cookies.set("avatar", value);
    },
    setRoute(state, value) {
      state.backRouter = value;
      localStorage.setItem('backRoute', value);
    },
    setAutomaticLogin(state, value) {
      state.automaticLogin = value;
      localStorage.setItem('automaticLogin', value)
    }
  },
  actions: {
    // 密码登录
    passwordLogins({ commit, state }, value) {
      passwordLogin(value).then(res => {
        if (res.data.resultCode == '200') {
          commit('setToken', res.data.data)
          toast('登陆成功', 2000, 'success');
          localStorage.removeItem("last");
          router.replace(state.backRouter)
        }
      })
    },
    // 手机验证码登录
    phoneCode({ commit, state }, value) {
      phoneCodeLogin({ mobile: value.phone, code: value.code }).then(res => {
        if (res.data.resultCode == '200') {
          commit('setToken', res.data.data)
          toast('登陆成功', 2000, 'success');
          localStorage.removeItem("last");
          router.replace(state.backRouter)
        }
      })
    },


    // 手机号注册
    phoneRegisters({ commit }, value) {
      phoneRegister(value).then(res => {
        if (res.data.resultCode == "200") {
          commit("addMessage", true)
        }
      })
    },
    // 邮箱注册
    emailRegisters({ commit }, value) {
      emailRegister(value).then(res => {
        if (res.data.resultCode == "200") {
          commit("addMessage", true)
        }
      })
    },
    // 手机号找回密码
    phoneRetrieves({ commit }, value) {
      phoneRetrieve(value).then(res => {
        if (res.data.resultCode === '200') {
          toast('修改密码成功', 2000, 'success');
        }
      })
    },

    // 通过邮箱找回密码
    emailRetrieves({ commit }, value) {
      emailRetrieve(value).then(res => {
        if (res.data.resultCode === '200') {
          toast('修改密码成功', 2000, 'success');
        }
      })
    },
    // 判断用户是否存在
    async checkUserExists({ commit, dispatch }, value) {
      let res = await checkUserExist({ account: (value.phone || value.eamil) })
      if (res.data.resultCode !== '200') {
        return;
      } else if (value.phone) {
        dispatch("phoneRetrieves", value)
      } else if (value.email) {
        dispatch("emailRetrieves", value)
      }
    },

  },
  modules: {
  }
})
